package day09;

public class HW5 {
/*需求描述：
定义方法 getNum(String type, int... numbers)，根据 type 参数对可变参数 numbers 执行指定计算：
支持操作类型："sum"（求和）、"avg"（算术平均）、"max"（最大值）、"min"（最小值）等
处理空参数集时返回 0
输出格式示例：getNum("avg", 2,4,6) → 输出 平均值是：4.0 */
    public static void main(String[] args) {
        // 测试示例
        System.out.println(getNum("sum", 1, 2, 3));
        System.out.println(getNum("avg", 4, 5, 6));
        System.out.println(getNum("max", 1, 2, 3));
        System.out.println(getNum("min", 4, 5, 6));
        System.out.println(getNum("sum")); 
    }
 
    public static String getNum(String type, int... numbers) {
        // 处理空参数集
        if (numbers.length == 0) {
            switch (type) {
                case "sum":
                    return "总和是：0";
                case "avg":
                    return "平均值是：0.0";
                case "max":
                    return "最大值是：0";
                case "min":
                    return "最小值是：0";
                default:
                    return "不支持的操作类型";
            }
        }
        
        switch (type) {
            case "sum":
                int sum = 0;
                for (int num : numbers) {
                    sum += num;
                }
                return "总和是：" + sum;
                
            case "avg":
                int total = 0;
                for (int num : numbers) {
                    total += num;
                }
                double average = (double) total / numbers.length;
                return "平均值是：" + average;
                
            case "max":
                int max = numbers[0];
                for (int i = 1; i < numbers.length; i++) {
                    if (numbers[i] > max) {
                        max = numbers[i];
                    }
                }
                return "最大值是：" + max;
                
            case "min":
                int min = numbers[0];
                for (int i = 1; i < numbers.length; i++) {
                    if (numbers[i] < min) {
                        min = numbers[i];
                    }
                }
                return "最小值是：" + min;
                
            default:
                return "不支持的操作类型";
        }
    }
}